package cn.com.elite;

import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import com.elite.algorithm.GUID;
import com.elitecrm.saas.service.EliteSAASException;

public class SaveInfoService {
	private static final Log log = LogFactory.getLog(SaveInfoService.class);
	
	public String saveInfoService(DataSource dataSource,String ep_id,Document doc,String Staff_ID)throws Exception{
		String ReturnXml="";
		Element ExamResultMainIDEle=(Element)doc.selectSingleNode("/Root/ExamResultMainID");
		if(ExamResultMainIDEle!=null){
			String ExamResultMainID=ExamResultMainIDEle.getText();
			if(ExamResultMainID!=null){
				Element ComputerNameEle=(Element)doc.selectSingleNode("/Root/ComputerName");
				String ComputerName=ComputerNameEle.getText();
				Element ComputerIPEle=(Element)doc.selectSingleNode("/Root/ComputerIP");
				String ComputerIP=ComputerIPEle.getText();
				ReturnXml=saveInfoServiceImpl(dataSource,ep_id,doc,ExamResultMainID,Staff_ID,ComputerName,ComputerIP);
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamResultMainID==null");
			}
		}else{
			throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"ExamResultMainIDEle==null");
		}
		return ReturnXml;
	}
	
	public String saveInfoServiceImpl(DataSource dataSource,String ep_id,Document doc,String ExamResultMainID,String Staff_ID,String ComputerName,String ComputerIP)throws Exception{
		String ReturnXml="";
		int right_num=0;
		int wrong_num=0;
		float score=0;
		int un_num=0;
		Element result_list=(Element)doc.selectSingleNode("/Root/ksresult");
		Iterator iters=result_list.elementIterator();
		while(iters.hasNext()){
			Element result=(Element)iters.next();
			
			Element ExamListID_Element=result.element("ExamListID");
			String ExamListID=ExamListID_Element.getText();
			log.debug("ExamListID:"+ExamListID);
			
			Element QListID_Element=result.element("QListID");
			String QListID=QListID_Element.getText();
			log.debug("QListID:"+QListID);
			
			Element ResultData_Element=result.element("ResultData");
			String ResultData=ResultData_Element.getText();
			log.debug("ResultData:"+ResultData);
			
			Element QResult_Element=result.element("QResult");
			String QResult=QResult_Element.getText();
			log.debug("QResult:"+QResult);
			
			Element QTypeKey_Element=result.element("QTypeKey");
			String QTypeKey=QTypeKey_Element.getText();
			log.debug("QTypeKey:"+QTypeKey);
			
			Element Score_Element=result.element("Score");
			String Score=Score_Element.getText();
			log.debug("Score:"+Score);
			
			GUID guid=new GUID();
			String ExamResultListID=guid.toString();
			String W_ExamResultList="W_ExamResultList_"+ep_id;
			String sql="insert into "+W_ExamResultList+"(ExamResultMainID,ExamResultListID,ExamListID,QListID,ResultData,Score,ResultStatus,MarkType)Values(?,?,?,?,?,?,?,?)";
			int[] types=new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.FLOAT,Types.INTEGER,Types.INTEGER};  
			if(QTypeKey.equals("P")){       //判断
				if(ResultData.equals(QResult)){
					right_num=right_num+1;
					score=score+Float.parseFloat(Score);
					Object[] params=new Object[]{ExamResultMainID,ExamResultListID,ExamListID,QListID,ResultData,Float.parseFloat(Score),1,1};
					util.UpdatePersonUseExecute(sql, types, params, dataSource);
				}else{
					wrong_num=wrong_num+1;
					Object[] params=new Object[]{ExamResultMainID,ExamResultListID,ExamListID,QListID,ResultData,Float.parseFloat(Score),0,1};
					util.UpdatePersonUseExecute(sql, types, params, dataSource);
				}
			}else if(QTypeKey.equals("S")){     //单选
				if(ResultData.equals(QResult)){
					right_num=right_num+1;
					score=score+Float.parseFloat(Score);
					Object[] params=new Object[]{ExamResultMainID,ExamResultListID,ExamListID,QListID,ResultData,Float.parseFloat(Score),1,1};
					util.UpdatePersonUseExecute(sql, types, params, dataSource);
				}else{
					wrong_num=wrong_num+1;
					Object[] params=new Object[]{ExamResultMainID,ExamResultListID,ExamListID,QListID,ResultData,Float.parseFloat(Score),0,1};
					util.UpdatePersonUseExecute(sql, types, params, dataSource);
				}
			}else if(QTypeKey.equals("M")){      //多选
				if(ResultData.equals(QResult)){
					right_num=right_num+1;
					score=score+Float.parseFloat(Score);
					Object[] params=new Object[]{ExamResultMainID,ExamResultListID,ExamListID,QListID,ResultData,Float.parseFloat(Score),1,1};
					util.UpdatePersonUseExecute(sql, types, params, dataSource);
				}else{
					wrong_num=wrong_num+1;
					Object[] params=new Object[]{ExamResultMainID,ExamResultListID,ExamListID,QListID,ResultData,Float.parseFloat(Score),0,1};
					util.UpdatePersonUseExecute(sql, types, params, dataSource);
				}
			}else{
				un_num=un_num+1;
				Object[] params=new Object[]{ExamResultMainID,ExamResultListID,ExamListID,QListID,ResultData,Float.parseFloat(Score),0,1};
				util.UpdatePersonUseExecute(sql, types, params, dataSource);
			}
			
		}
		String W_ExamResultMain="W_ExamResultMain_"+ep_id;
		String sql1="update "+W_ExamResultMain+" set EndTime=?,MarkScore=?,ComputerName=?,ComputerIP=? where ExamResultMainID=?";
		int[] types1=new int[]{Types.TIMESTAMP,Types.FLOAT,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String EndTime=df.format(new Date());
		log.debug("ExamResultMainID:"+ExamResultMainID);
		Object[] params1=new Object[]{EndTime,score,ComputerName,ComputerIP,ExamResultMainID};
		int update_result=util.UpdatePersonUseExecute(sql1,types1,params1,dataSource);
		log.debug("update_result:"+update_result);
		
		
		Document result=util.getDoc();
		Element root=result.addElement("R");
		root.addElement("Result").addText("1");
		root.addElement("RightQuantity").addText(right_num+"");
		root.addElement("WrongQuantity").addText(wrong_num+"");
		root.addElement("UnJudgeQuantity").addText(un_num+"");
		root.addElement("Score").addText(score+"");

        String sql2="select * from "+W_ExamResultMain+" where ExamResultMainID=? and Staff_ID=?";
		int[] types2=new int[]{Types.VARCHAR,Types.VARCHAR};
		Object[] params2=new Object[]{ExamResultMainID,Staff_ID};
		List list=util.QueryPersonUseExecute(sql2, types2, params2, dataSource);
		if(list!=null&&list.size()>0){
			root.addElement("KSHistory").addText("1");
		}else{
			root.addElement("KSHistory").addText("0");
		}
		ReturnXml=result.asXML();
		return ReturnXml;
	}
	
}
